test!!

//creare document pe client
//v1- functionalitate doar pt Mozilla

function xmlDocument(docName, url){
	/**
	* constructor
	* @param String url adresa relativa a adocumentului ce va fi incarcat
	* @param String docName numele documentului
	* @return void
	*/
	var docType=document.implementation.createDocumentType("xmlDocument", null, "../../include/dtd/docSkeleton.dtd");
	this.docHandler=document.implementation.createDocument(null, "xmlDocument", docType);
	this.docHandler.async=false;
	this.docName=docName;
	/**
	* elementele documentului
	* va contine doar elementele noi create sau cele asupra
	* carora s'au realizat modificari
	*/
	this.docElements=new Array();
	this.elementsCounter=0;

	if(url == null){
		this.docRoot=this.docHandler.documentElement;
		this.docRoot.appendChild(this.docHandler.createElement('docInfo'));
		this.docContentRoot=this.docHandler.createElement('docBody');
		this.docRoot.appendChild(this.docContentRoot);
	}else{
		//load document from surce
		this.docHandler.load(url+"/"+docName+".xml");
		this.docURL=url;
		this.docRoot=this.docHandler.documentElement;
		var nodeList=this.docHandler.getElementsByTagName('docBody');
		this.docContentRoot=nodeList.item(0);
		//alert('load from url: '+url);
	}
	checkBrowser();

	/**
	* afisare continut document in alert
	*/
	this.printDocument=function(){
		alert(this.docHandler.xml);
	}

	/**
	* creare capitol nou
	*/
	this.createNewChapter=function(chapName, chapId, chapUrl){
		try{
			//alert('1');
			var newChapter=new xmlChapter(chapName, chapId, chapUrl);
			//adaugare la structura actuala a documentului
			var newChapterContent=this.docHandler.importNode(newChapter.chapRoot, true);
			//alert('3');
			//alert(newChapterContent.xml);
			this.docContentRoot.appendChild(newChapterContent);
			//return newChapter;*/
			//alert('insert ok ');
			this.docElements[chapName]=newChapter;
			//this.docElements[this.elementsCounter]=newChapter;
			this.elementsCounter+=1;
		}catch(e){
			alert("Could not create new chapter");
			return false;
		}
	}

	/**
	*	incarcare capitol de pe server
	*/
	this.loadChapter=function(chapter){
		//checkBrowser();
		try{
			var url="documente/"+this.docName+"/"+chapter+".xml";
			//alert(url);
			//var newChapter=new XmlDom();
			var newChapter=xmlChapter(chapter, this.generateRandomNumber(), url);
			//newChapter.async=false;
			//newChapter.load(url);
			//alert("1");
			//alert(newChapter.xml);
			var newChapterContent=this.docHandler.importNode(newChapter.chapRoot, true);
			//alert("2");
			this.docContentRoot.appendChild(newChapterContent);
			//alert("3");
			//this.docElements[this.elementsCounter]=newChapter;
			this.docElements[chapter]=newChapter;
			this.elementsCounter+=1;
		}catch(e){
			alert("Could not load chapter");
			return false;
		}
	}

	/**
	*	afisare continut
	*/
	this.printToHtml=function(){
		var XslDom=new XmlDom();
		XslDom.async=false;
		XslDom.load('include/xsl/chapters.xsl');
		document.getElementById('helder').innerHTML=this.docHandler.transformNode(XslDom);
	}
	
	/*this.printCommentedToHtml=function(){
		var XslDom=new XmlDom();
		XslDom.async=false;
		XslDom.load('include/xsl/xmlChaptersComments.xsl');
		document.getElementById('helder').innerHTML=this.docHandler.transformNode(XslDom);
		alert("dfgsdfg");
	}*/
	
	/**
	*	regasire element dupa id
	*/
	this.fetchElementById=function(id){
		//var elem=this.docContentRoot.getElementById(id);
		//return elem;
	}

	this.generateRandomNumber=function(){
		A=99;
		B=999999999;
		num =Math.round( A + (B-A)*Math.random());
		return num;
	}
	
	return this;
}




function testUnit(){
	checkBrowser();
	var unu=new xmlDocument('teste2','documente/teste2/teste2.xml');
	//var unu=new xmlDocument('unu', null);
	//unu.printDocument();
	//var p1=unu.createNewChapter('chTestJS', 'chTestJSId', null);
	//var p2=unu.createNewChapter('chTest2JS', 'chTest2JSId', null);
	//alert(unu.xml);
	unu.printDocument();
	/*	try{
	//var p1=unu.createNewChapter('chTestJS', 'chTestJSId', null);
	unu.docElements[0].createNewParagraph('p1','idP1');
	}catch(e){
	alert("-----------><--------");
	}
	//unu.docElements[0].printDocument();

	//alert(unu.docContentRoot.xml);

	/*	try{
	myJsonObject=xml2json.parser(unu.docContentRoot.xml,'', 'compact');
	}catch(e){
	alert(e.message);
	}
	alert(myJsonObject);*/
	//	var Xmldom=new XmlDom();
	//var XslDom=new XmlDom();
	//var XslDom=document.implementation.createDocument(null, null, null);
	//	Xmldom.async=false;
	//XslDom.async=false;
	//	Xmldom.loadXML(unu.docHandler.xml);
	//XslDom.load('xml2json.xsl');

	/*var newProcessor=new XSLTProcessor();
	newProcessor.importStylesheet(XslDom);

	var newResultDom=newProcessor.transformToDocument(Xmldom);
	var newResult=newResultDom.xml;  */
	//    var newResult=Xmldom.transformNodeToDoc('xml2json.xsl');

	///alert(newResult);
	/*'labelAttr': 'name',
	'typeAttr': 'type',
	'store': store,
	'query': {type : 'continent'}*/
	//document.write(Xmldom.xml);
	//alert(unu.docRoot.transformNode(XslDom));
	//return newResult;
	//	var newResParsed=eval('('+newResult+')');
	//alert('end eval');

	//	return newResParsed;
	/*	var menuStore=new dojo.data.ItemFileReadStore({'data': newResParsed});
	alert('1');
	///<div dojoType="dijit.Tree" store="menuStore" labelAttr="name" id="tree">
	var newTree=new dijit.Tree({'labelAttr' : 'name', 'typeAttr' : 'type', 'store' : 'menuStore', 'query' : {type: 'chapter'}}, dojo.byId('tree'));
	alert('2');
	newTree.store=menuStore;
	alert('end');
	//newTree.startup();*/

}

function loadDocumentFromUrl(name){
	checkBrowser();
	//alert(name);
	var url="documente/"+name+"/"+name+".xml";
	//var docsi=new xmlDocument(name,url);
	//docsi.printDocument();
	//alert(url);
	var Xmldom=new XmlDom();
	var XslDom=new XmlDom();

	Xmldom.async=false;
	XslDom.async=false;

	Xmldom.load(url);
	XslDom.load('include/xsl/docInfo.xsl');

	document.getElementById('helder').innerHTML=Xmldom.transformNode(XslDom);
	//alert(Xmldom.transformNode(XslDom));
}

function loadChapterFromUrl(chapter, doc){
	checkBrowser();
	//alert(name);
	var url="documente/"+doc+"/"+chapter+".xml";
	//var docsi=new xmlDocument(name,url);
	//docsi.printDocument();
	//alert(url);
	var Xmldom=new XmlDom();
	var XslDom=new XmlDom();

	//alert(Xmldom.xml);

	Xmldom.async=false;
	XslDom.async=false;

	Xmldom.load(url);
	XslDom.load('include/xsl/chapters.xsl');

	//alert(Xmldom.xml);

	document.getElementById('helder').innerHTML=Xmldom.transformNode(XslDom);
	//alert(Xmldom.transformNode(XslDom));
}